import axios from 'axios'
import { Toast } from 'vant'
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_URL,
  timeout: 1000 * 10 * 1, // 分钟
  headers: {
    'Content-Type': 'application/json'
  }
})

// 请求拦截
service.interceptors.request.use(
  config => {
    if (!config.headers['Authorization']) {
      // 带token
      if (sessionStorage.token) {
        config.headers['Authorization'] = sessionStorage.token
      }
    }
    return config
  },
  error => {
    Toast({
      type: 'fail',
      message: '请求失败!'
    })
    return Promise.reject(error)
  }
)

// 响应拦截
service.interceptors.response.use(
  response => {
    if (response.status && response.status !== 200) {
      Toast({
        type: 'fail',
        message: response.statusText || '业务异常！'
      })
    }
    return response.data
  },
  err => {
    Toast({
      type: 'fail',
      message: '请求失败！'
    })
    return Promise.reject(err)
  }
)

export function get(url, params) {
  return service({
    url: url,
    params: {
      ...params,
      // token: localStorage.getItem('token')
    },
    method: 'get'
  })
}

export function post(url, data) {
  return service({
    url: url,
    data: {
      data,
      // token: localStorage.getItem('token')
    },
    method: 'post'
  })
}
// export function get(url, params) {
//   const newParams = {
//     ...params,
//     token: store.state.token
//   }
//   return service({
//     url: url,
//     params: newParams,
//     method: 'get'
//   })
// }
